package band.wukong.practice.javase.core.io.readerandwriter;

import java.io.*;

/**
 * Desc:
 * Author: sunjs
 * Date: 2009-11-17 9:05:02
 */
public class TestReadFile {
//    static final String FILE_IN = "E:\\workspace\\TestJava\\src\\com\\jsun\\javase\\io\\doc\\poem.txt";
//    static final String FILE_OUT = "E:\\workspace\\TestJava\\src\\com\\jsun\\javase\\io\\doc\\out.txt";

    static final String FILE_IN = "E:\\ws_test\\TestJava\\src\\com\\jsun\\javase\\corepackage\\io\\testdir\\poem.txt";
    static final String FILE_OUT = "E:\\ws_test\\TestJava\\src\\com\\jsun\\javase\\corepackage\\io\\testdir\\out.txt";

    /**
     * 读取文件
     *
     * @param fileName 文件名称
     * @throws Exception
     */
    public static void readFile(String fileName) throws Exception {
        readFile(fileName, null);
    }

    /**
     * 读取文件，按指定字符集
     *
     * @param fileName    文件名称
     * @param charsetName 字符集
     * @throws Exception
     */
    public static void readFile(String fileName, String charsetName) throws Exception {
        InputStream in = new FileInputStream(fileName);
        InputStreamReader reader;
        if (null == charsetName) {
            reader = new InputStreamReader(in);
        } else {
            reader = new InputStreamReader(in, charsetName);
        }
        BufferedReader br = new BufferedReader(reader);
        String data;
        while ((data = br.readLine()) != null) {
            System.out.println(data);
        }
        br.close();
    }

    public static void main(String[] args) throws Exception {
//        readFile(FILE_IN);
//        readFile(FILE_IN, "UTF-8");
    }
}
